<?php


namespace App\Models\CacheModel;


use app\common\lib\exception\ApiException;
use app\common\lib\Util;
use App\Models\Api\BaseCache;
use think\Cache;
use think\Log;
use AstroTool\Cache\CacheManage;

class ZlAppleidUser extends BaseCache
{
    protected $table = 'wechat_backup_07_18_14_43_00.zl_appleid_user';
    protected $cacheModel = 'user.apple';

    public function SaveData($data)
    {
        $res = Cache::get($data['apple_id']);
        if ($res)
        {
            throw new ApiException('当前账号已注册');
        }
        Cache::set($data['apple_id'], 1, 2);
        try
        {
            $chennelId = Util::getChannelId();
            $data['channel_id'] = $chennelId;
            $this->allowField(true)->isUpdate(false)->save($data);
        }
        catch (\Exception $e)
        {
            Log::write('添加用户apple信息失败：' . $e->getMessage(), 'error', true);
            return false;
        }
        return true;
    }

    /**
     * 更新apple信息
     * @param $data
     * @param $map
     * @param string $appleId
     * @return bool
     * @throws \Exception
     */
    public function UpdateData($data, $map, string $appleId)
    {
        try
        {
            $this->where($map)->update($data);
        }
        catch (\Exception $e)
        {
            return false;
        }
        CacheManage::getInstance()->setCacheConfig($this->cacheModel, $appleId)->rmCache();
        return true;
    }

    /**
     * 解绑数据
     * @param int $uid
     * @return bool
     * @throws \Exception
     */
    public function delData(int $uid)
    {
        $apple_id = $this->getAppleId($uid);
        try
        {
            $this->where('uid', $uid)->delete();
        }
        catch (\Exception $e)
        {
            return false;
        }

        if ($apple_id)
        {
            $cacheKey = 'user:has_bind:apple_id:' . $uid;
            Cache::rm($cacheKey);
            CacheManage::getInstance()->setCacheConfig($this->cacheModel, $apple_id)->rmCache();
        }
        return true;
    }

    /**
     * 缓存获取用户Apple信息
     * @param string $appleId
     * @return array|bool|mixed
     * @throws ApiException
     */
    public function getUserInfo(string $appleId)
    {
        $data = CacheManage::getInstance()->setCacheConfig($this->cacheModel, $appleId)->getDataAndSet(function () use ($appleId)
        {
            return $this->getAppleInfoFromDms($appleId);
        });

        if ($data && $data['forbidden'] == 1)
        {
            throw new ApiException('该账号禁止登录', 200, 0);
        }
        return $data;
    }

    /**
     * 数据库获取用户Apple信息
     * @param string $appleId
     * @return array|mixed
     */
    public function getAppleInfoFromDms(string $appleId)
    {
        try
        {
            $data = $this->alias('a')->join('wechat_backup_07_18_14_43_00.app_user u', 'a.uid=u.id')
                ->where('apple_id', $appleId)->column('a.uid,a.nickname,a.email,a.create_time,u.forbidden,u.has_self_recode,u.avatar');
            $data = current($data);
        }
        catch (\Exception $e)
        {
            $data = [];
        }

        return $data;
    }

    /**
     * 是否绑定苹果ID
     * @param $uid
     * @return int
     */
    public function getAppleId($uid)
    {
        $cacheKey = 'user:has_bind:apple_id:' . $uid;
        $apple_id = Cache::get($cacheKey);

        if ($apple_id === false)
        {
            $apple_id = $this->where('uid', $uid)->value('apple_id', '');

            Cache::set($cacheKey, $apple_id, 86400 * 7);
        }

        return $apple_id;
    }
}
